package com.hula.onion.book.server.dao.impl;

import com.hula.onion.book.server.dao.IAccountInfoDao;
import com.hula.onion.book.server.model.AccountInfo;
import com.hula.onion.book.server.vo.AccountInfoVo;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.util.List;

/**
 * Created by work on 2018/1/29.
 */
@Repository
public class AccountInfoDaoImpl extends JdbcDaoSupport implements IAccountInfoDao {
    @Resource(name = "dataSource")
    private DataSource dataSource;

    private static final BeanPropertyRowMapper<AccountInfoVo> mapperVo =
            new BeanPropertyRowMapper<>(AccountInfoVo.class);
    private static final BeanPropertyRowMapper<AccountInfo> mapper =
            new BeanPropertyRowMapper<>(AccountInfo.class);

    AccountInfoDaoImpl(DataSource dataSource) {
        setDataSource(dataSource);
    }


    private static final String UPDATE_INFO = "update p2p_investment_info set amount=?" +
            ",yesterday_intrest=?,total_intrest=?,wait_intrest=? where user_id=? and platform_id=?;";

    @Override
    public boolean updatePlatformAccount(int userId, int platformId, AccountInfo accountInfo) {
        return 1 == this.getJdbcTemplate().update(UPDATE_INFO,
                new Object[]{accountInfo.getAmount(),
                        accountInfo.getYesterdayIntrest(),
                        accountInfo.getTotalIntrest(),
                        accountInfo.getWaitIntrest(),
                        userId, platformId});
    }

    private static final String CREATE_INFO = "insert into " +
            "p2p_investment_info(user_id,platform_id,amount," +
            "yesterday_intrest,total_intrest,wait_intrest,auto_sync) values(?,?,?,?,?,?,?);";

    @Override
    public boolean createPlatformAccount(int userId, int platformId, int syncType, AccountInfo accountInfo) {
        return 1 == this.getJdbcTemplate().update(CREATE_INFO,
                new Object[]{userId, platformId,
                        accountInfo.getAmount(),
                        accountInfo.getYesterdayIntrest(),
                        accountInfo.getTotalIntrest(),
                        accountInfo.getWaitIntrest(), syncType});
    }

    private static final String USER_ACCOUNT_INFO = "select p.id platform_id,pi.id invest_id,pi.amount,pi.auto_sync,p.name,p.icon_url,p.show_captcha from p2p_investment_info pi left join platform_info p on pi.platform_id=p.id where user_id=?";

    @Override
    public List<AccountInfoVo> getUserAccounts(int userId) {
        return this.getJdbcTemplate().query(USER_ACCOUNT_INFO, new Object[]{userId}, mapperVo);
    }

    private static final String USER_PLATFORM_ACCOUNT_INFO = "select * from p2p_investment_info where id=?";

    @Override
    public AccountInfo getUserPlatformAccount(int investId) {
        List<AccountInfo> result = this.getJdbcTemplate().query(USER_PLATFORM_ACCOUNT_INFO,
                new Object[]{investId}, mapper);
        if (CollectionUtils.isNotEmpty(result)) {
            return result.get(0);
        }
        return null;
    }

    private static final String SELECT_BY_USERID_PLATFORMID = "select * from p2p_investment_info where user_id=? and platform_id =?";

    @Override
    public AccountInfo queryByUserIdAndPlatformId(int userId, int platformId) {

        List<AccountInfo> result = this.getJdbcTemplate().
                query(SELECT_BY_USERID_PLATFORMID, new Object[]{userId, platformId}, mapper);

        if (result != null && result.size() > 0) {
            return result.get(0);
        }
        return null;
    }

    private static final String DELETE_BY_ID = "delete from p2p_investment_info where id = ?";

    @Override
    public int delUserPlatformAccountById(int id) {
        return this.getJdbcTemplate().update(DELETE_BY_ID, new Object[]{id});
    }

    private static final String HAVE_RECORD = "select count(*) from p2p_investment_info where user_id=? and platform_id=?;";

    @Override
    public boolean haveRecord(int userId, int platformId) {
        List<Integer> result = this.getJdbcTemplate().queryForList(HAVE_RECORD,
                new Object[]{userId, platformId}, Integer.class);
        if (CollectionUtils.isEmpty(result)) {
            return false;
        }
        return true;
    }
}
